/**
 * Copyright (c) 2011-2016, Eason Pan(pylxyhome@vip.qq.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jcbase.controller.sys;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.jcbase.core.auth.anno.RequiresPermissions;
import com.jcbase.core.controller.JCBaseController;
import com.jcbase.core.pojo.Syslog;
import com.jcbase.core.util.JqGridModelUtils;
import com.jcbase.core.util.StringUtils;
import com.jcbase.core.view.InvokeResult;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

/**
 * 日志管理.
 *
 * @author eason
 */
public class LogController extends JCBaseController {

    @RequiresPermissions
    public void index() {
	render("log_index.jsp");
    }

    @RequiresPermissions(value = { "/sys/log" })
    public void getListData() {

	String UserName = getPara("UserName");
	String operation = getPara("operation");
	String startTime = getPara("start_time");
	String endTime = getPara("end_time");

	int totalRow;
	int totalPage;
	HashMap<String, String> map = getOpMap();
	StringBuilder sqlSel = new StringBuilder();
	StringBuilder sqlCount = new StringBuilder();
	StringBuilder sql = new StringBuilder();
	List<Record> Recordlist = new ArrayList<Record>();
	sqlCount.append("select count(*) ");
	sqlSel.append("select sl.*,su.name as userName  ");
	sql.append(" from sys_log sl left join  sys_user su on sl.uid=su.id where 1=1 ");
	if (!StringUtils.isBlank(UserName)) {
	    sql.append(" and  su.class_name = ");
	    sql.append("'" + UserName + "'");
	}
	if (!StringUtils.isBlank(operation)) {
	    sql.append(" and  sl.url = ");
	    sql.append("'" + operation + "'");
	}
	if (!StringUtils.isBlank(startTime)) {
	    sql.append(" and  sl.start_time >= ");
	    sql.append(startTime);
	}
	if (!StringUtils.isBlank(endTime)) {
	    sql.append(" and  sl.start_time <= ");
	    sql.append(endTime);
	}
	Recordlist = Db.find(sqlSel.append(sql.toString()).toString());
	List<Record> RecordCount = Db.find(sqlCount.append(sql.toString()).toString());
	totalRow = RecordCount.size();
	if (totalRow == 0) {
	    totalPage = 1;
	} else {
	    totalPage = totalRow / this.getRows() + 1;
	}
	List<Syslog> logList = new ArrayList<Syslog>();
	for (Record record : Recordlist) {
	    Syslog syslog = new Syslog();
	    syslog.setUserName(record.getStr("class_name"));
	    if (map.get(record.getStr("url")) != null) {
		syslog.setOperation(map.get(record.getStr("url")));
	    }
	    syslog.setStartTime(record.getDate("start_time"));
	    logList.add(syslog);
	}

	Page<Syslog> pageInfo = new Page<Syslog>(logList, this.getPage(), this.getRows(), totalPage, totalRow);
	this.renderJson(JqGridModelUtils.toJqGridView(pageInfo));
    }

    // 获取操作列表
    @RequiresPermissions(value = { "/sys/log" })
    public void getOpListData() {

	this.renderJson(InvokeResult.success(getOpMap()));
    }

    public static HashMap<String, String> getOpMap() {
	HashMap<String, String> map = new HashMap<String, String>();
	map.put("/dologin", "登录");
	map.put("/loginOut", "退出");
	map.put("/savePwdUpdate", "密码修改");
	map.put("/sys/objShare/getShareOtherObjList", "分享他人");
	map.put("/sys/objShare/getShareMeObjList", "分享给我");
	map.put("/sys/objShare/shareObjCheck", "对象审批");
	return map;
    }

}
